import React from 'react';

import CKModal from '../components/CKModal';
import CKTable from '../components/CKTable';
import CKPages from '../components/CKPages';
import AMUIReact, {
    Button,
    Input
} from 'amazeui-react';
import Fetch from '../common/Fetch';

class Category extends React.Component {
    constructor(props) {
        super(props);

        this.state = {
            data: [],
            page: {
                pageSize: 20,
                currentPage: 1,
                totalNum: 43
            },
            dataCount: 1,
            pageNum: 10,
        };
    }

    componentDidMount() {
        this.loadCategory(1);
    }

    loadCategory(page) {
        Fetch('/cms/article_category_manage/query', {
            query: {tmp_name:{
                name: 'ctg_name',
                value: this.refs.name.getValue() + '%',
                type: 'like'
            }}, num: this.state.pageNum, page: page
        }, (res)=> {
            if (res.status) {
                this.setState({
                    data: res.data.data,
                    currentPage: page,
                    dataCount: res.data.count
                });
            }
        });
    }

    clickHandler = (row) => {
        this.props.callback(row);
    };

    render() {
        return (
            <div>
                <div>
                    <p>
                        <Input ref="name"
                            placeholder="请输入分类"
                               labelClassName="am-u-sm-3"
                               wrapperClassName="am-u-sm-4 am-u-end"/>
                        <Button onClick={()=> {this.loadCategory(1)}} labelClassName="am-u-sm-2"
                                wrapperClassName="am-u-sm-4 am-u-end" amStyle="success" radius>查询</Button>
                    </p>
                    <p>
                        <CKTable ref="table" data={this.state.data} select={false}>
                            {/*<CKTable.Header text="ID" field="ctg_id"/>*/}
                            <CKTable.Header text="分类名称" field="ctg_name"/>
                            <CKTable.Header text="所属上级" field="ctg_parent_name"/>
                            <CKTable.Header text="发布目录" field="ctg_publish_path"/>
                            <CKTable.Header text="操作">
                                <Button onClick={this.clickHandler} amSize="xs" amStyle="primary" radius>选择</Button>
                            </CKTable.Header>
                        </CKTable>
                        <CKPages current={this.state.currentPage}
                                 number={this.state.pageNum}
                                 count={this.state.dataCount}
                                 onSelect={(page)=> {this.loadCategory(page)}}/>
                    </p>
                </div>
                <CKModal ref="modal"/>
            </div>
        );
    }
}

export default Category;